package com.koicarp.agent.common.tools;

/**
 * 字节转换
 */
public class ByteConvertUtil {
    
    private ByteConvertUtil() {
        throw new IllegalStateException("实用工具类不应具有公共构造函数");
    }
    
	private static final String BYTE_UNIT = "B";
	
	private static final String KB_UNIT = " KB";

	private static final String KMGTPE = "KMGTPE";

    public static String formatBytes(long bytes) {
    	if (bytes == 0) {
    		return bytes + KB_UNIT;
    	}
    	
        if (bytes <= 1024) {
        	return "1.00" + KB_UNIT;
        }
        
        int exp = (int) (Math.log(bytes) / Math.log(1024));
        String pre = KMGTPE.charAt(exp-1) + BYTE_UNIT;
        return String.format("%.2f %s", bytes / Math.pow(1024, exp), pre);
    }
}
